package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap;

import com.alibaba.cirtus.ots.application.adapter.ability.model.Result;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpTokenUtil;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpUtils;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.otsapplicationadaptersdk.contants.OtsApplicationAdapterSdkConstants;
import com.epoch.app.otsapplicationadaptersdk.mnsapif034v2.service.MnSapIf034V2Service;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnSapIf034Request;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommResponse;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommSDO;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * SAP交货单删除回传中台（F-B）
 * SAP->中台
 * @author baizhuo
 * @version 1.0
 * @date 2024/1/8 21:28
 */
@Component
@Primary
public class MnSapIf034V2ServiceImpl implements MnSapIf034V2Service {

    private final Log log = Log.getLogger(MnSapIf034V2ServiceImpl.class);

    @Autowired
    private HttpUtils httpUtils;

    @Override
    @ProcessInvoker(remark = "MnSapIf034V2Service", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public MnSapCommResponse deliveryOrderFBDeleteFeedback(MnSapIf034Request mnSapIf034Request) {
        log.info("MnSapIf034V2Service deliveryOrderFBDeleteFeedback mnSapIf034Request:" + JSONObject.toJSONString(mnSapIf034Request));
        MnSapCommResponse response = new MnSapCommResponse();
        response.setInterfacePath(mnSapIf034Request.getMessageHeader().getINTERFACE());
        response.setMessageId(mnSapIf034Request.getMessageHeader().getMESSAGEID());
        List<MnSapCommSDO> mnSapCommSDOS = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sapDeliveryOrderNoList", Lists.newArrayList(mnSapIf034Request.getDeliverOrderNo()));
            Map<String, String> header = HttpTokenUtil.generateHttpHeader();
            String urlPrefix = OtsApplicationAdapterSdkConstants.urlPrefix();
            String url = urlPrefix + "api/mn_bc_link/inOutBoundOrderDeleteFromSap";
            log.info("MnSapIf034V2ServiceImpl deliveryOrderFBDeleteFeedback call bc link interface,url:{}, header:{}, param:{}", url, header, JSON.toJSONString(jsonObject));
            ResponseEntity<Result> post = httpUtils.post(url, header, jsonObject, Result.class);
            Result result = post.getBody();
            log.info("MnSapIf034V2ServiceImpl deliveryOrderFBDeleteFeedback, result:{}", JSON.toJSONString(result));
        } catch (Exception e) {
            log.error("MnSapIf034V2ServiceImpl deliveryOrderFBDeleteFeedback error", e);
        }
        return response;
    }

}
